// pages/rank/rank.js
function hashmapfn(){
  var p={
    rkey:7,
    size:0,
    obj:{}
  };

  function getKey(key){
    if(typeof key=='object'||typeof key==='function') return key.__key;
    return key;
  }
  function autokey(){
    var key="diqye_$";
    key+=p.rkey;
    p.rkey++;
    return key;
  }
  function setKey(key){
    if(typeof key=='object'||typeof key==='function') return key.__key=autokey();
    return key;
  }
  function contains(key){
    return p.obj[getKey(key)]!=null;
  }
  function put(key,val){
    if(!contains(key))p.size++;
    p.obj[setKey(key)]=val;
  }
  function get(key){
    return p.obj[getKey(key)];
  }
  function remove(key){
    if(contains(key)){
      p.size--;
      delete p.obj[getKey(key)];
    }
  }
  function size(){
    return p.size;
  }
  return {
    contains:contains,
    get:get,
    put:put,
    size:size,
    remove:remove,
    p:p
  }

}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    nickname: "",
    msgList: [],
    day: 0,
    currentId: 0,
    arr: [],
    hashmap: hashmapfn()

  },
  // 选择排行榜
  selectTouch:function(e) {
    this.setData({
      currentId:e.currentTarget.dataset.num
    })
    var day = 1;
    if(currentId == 1)day = 7;
    if(currentId == 2)day = 30;
    console.log()
  },
  getRank(day) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://47.95.223.130:8083/Rank/query',
        data: {
          day: day
        },
        method: "GET",
        success: res => {
          console.log(day);
          resolve(res)
        },
        fail: res => {
          reject('失败啦')
        }
      })
    })
  },
  getCountRank(hashmap) {
    return new Promise((resolve, reject) => {
      for (let i = 0; i < 30; i++) {
        var that = this;
        this.getRank(i).then(res => { 
          console.log(res);
          res.data.data.forEach(function(item, index) {
            hashmap.put(item.users_nickname,hashmap.contains(item.users_nickname)==true?hashmap.get(item.users_nickname):0 + item.quantity)
            console.log(item.users_nickname + " "+hashmap.get(item.users_nickname));
            that.setData({
              hashmap:hashmap
            })
          });
        }).catch(res => {
          console.log(res)	 
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var hashmap = this.data.hashmap;
    var arr = [];
    this.getCountRank(hashmap).then(res => {	
      console.log(1111111);
      arr = Object.entries(hashmap.p.obj).sort((a, b) => b[1] - a[1]);
    // let arr = Object.values(hashmap.p.obj).sort((a, b) => b - a);
    console.log(hashmap.get("qqqq"));
    console.log(arr);
    }).catch(res => {
      console.log(res)	
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.request({
    //   url: 'http://47.95.223.130:8083/Rank/queryToday',
    //   data: {
    //     nickname: this.data.nickname
    //   },
    //   method: "GET",
    //   success: (res) => {
    //     console.log(res);
    //   }
    // })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})